import service from 'axios'
import { service as axios, queryString, md5 } from '@vimi/utils-api'
import { PASSWORD_MD5 } from '@/common/plugins/constant'

export const pwdMd5 = pwd => md5(`${pwd}${PASSWORD_MD5}`)

export const authLogin = data =>
  axios.post('/auth/login/', {
    ...data,
    loginSecret: pwdMd5(data.loginSecret),
  })
export const authLogout = () => axios.post('/auth/logout/')

export const putOrg = data => axios.put('/auth/org/', data)
export const createOrgCorp = data => axios.post('/auth/org/corp/', data)
export const getOrgInfo = clientId => axios.get(`/auth/org/?clientId=${clientId}`)

export const authUserMe = () => axios.get('/auth/user/me/')
export const getUser = params => axios.get('/auth/user/', { params: params })
export const putUser = data => axios.put('/auth/user/', data)
export const createUser = data =>
  axios.post('/auth/user/operator/', {
    ...data,
    password: pwdMd5(data.password),
  })
export const deleteUser = userId => axios.delete(`/auth/user/?userId=${userId}`)
export const resetPassword = data =>
  axios.put(
    `/auth/user/password/${queryString({
      ...data,
      oldPassword: pwdMd5(data.oldPassword),
      newPassword: pwdMd5(data.newPassword),
    })}`
  )

export const authOrg = () => axios.get('/auth/org/')
export const getDept = data => axios.get(`/auth/org/dept/${queryString(data)}`)
export const createDept = data => axios.post('/auth/org/dept/', data)
export const updateDept = data => axios.put('/auth/org/dept/', data)
export const deleteDept = data => axios.delete(`/auth/org/dept/${queryString(data)}`)
export const getStaff = params => axios.get(`/auth/org/dept/emp/${queryString(params)}`)
export const getRefreshToken = refreshToken =>
  service.post(`/auth/refresh/?refresh_token=${refreshToken}`)
export const createMessage = data => axios.post('/auth/my/message/', data)
